package com.connector.server.redis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisPoolConfig;
import org.slf4j.LoggerFactory;
import com.connector.base.config.InstantConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import org.slf4j.Logger;

public class RedisPoolConfig
{
    private static final Logger log;
    public static JedisPool jedisPool;
    
    public static Jedis getJedis() {
        final Jedis jedis = new Jedis(InstantConfig.server.redis.url, InstantConfig.server.redis.port);
        jedis.auth(InstantConfig.server.redis.password);
        return jedis;
    }
    
    public static Jedis getResource() {
        Jedis jedis = null;
        try {
            if (RedisPoolConfig.jedisPool != null) {
                jedis = RedisPoolConfig.jedisPool.getResource();
            }
            return jedis;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)RedisPoolConfig.class);
        final JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10000);
        config.setMaxIdle(10);
        config.setMaxWaitMillis(60L);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        config.setTestWhileIdle(true);
        config.setMinEvictableIdleTimeMillis(6000L);
        config.setTimeBetweenEvictionRunsMillis(60000L);
        config.setNumTestsPerEvictionRun(-1);
        RedisPoolConfig.jedisPool = new JedisPool((GenericObjectPoolConfig)config, InstantConfig.server.redis.url, InstantConfig.server.redis.port, InstantConfig.server.redis.timeout, InstantConfig.server.redis.password, InstantConfig.server.redis.database);
    }
}
